package com.sgl.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.*;

/**
 * 签名工具类
 */
public class StringSingUtils {

	private static final Log log = LogFactory.getLog(StringSingUtils.class);

	/**
	 * 获取参数签名
	 * 
	 * @param paramMap
	 *            签名参数
	 * @param paySecret
	 *            签名密钥
	 * @return
	 */
	public static String getSign(Map<String, String> paramMap, String paySecret) {
		if (org.apache.commons.lang3.StringUtils.isNotEmpty(paramMap.get("sign"))){
			paramMap.remove("sign");
		}
		SortedMap<String, Object> smap = new TreeMap<String, Object>(paramMap);
		StringBuffer stringBuffer = new StringBuffer();

		for (Map.Entry<String, Object> m : smap.entrySet()) {
			Object value = m.getValue();
			if (value != null && org.apache.commons.lang.StringUtils.isNotBlank(String.valueOf(value))) {
				stringBuffer.append(m.getKey()).append("=")
						.append(m.getValue()).append("&");
			}
		}
		stringBuffer.delete(stringBuffer.length() - 1, stringBuffer.length());

		String argPreSign = stringBuffer.append("&secretKey=")
				.append(paySecret).toString();
		log.info("MD5原文:" + argPreSign);
		String signStr = MD5Util.encode(argPreSign).toUpperCase();
		log.info("MD5值：" + signStr);
		return signStr;
	}
	public static String getSign2(Map<String, String> paramMap, String secid_key) {
		List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(paramMap.entrySet());
		// 构造签名键值对的格式
		StringBuilder stringBuffer = new StringBuilder();
		for (Map.Entry<String, String> item : infoIds) {
			if (item.getKey() != null || item.getKey() != "") {
				String key = item.getKey();
				String val = item.getValue();
				if (!(val == "" || val == null)) {
					stringBuffer.append(key + "=" + val + "&");
				}
			}
		}
		stringBuffer.delete(stringBuffer.length() - 1, stringBuffer.length());

		String argPreSign = stringBuffer.append(secid_key).toString();
		log.info("MD5原文:" + argPreSign);
		String signStr = MD5Util.encode(argPreSign).toUpperCase();
		log.info("MD5值：" + signStr);
		return signStr;
	}
	public static String getSign(Map<String, String> paramMap) {
		SortedMap<String, Object> smap = new TreeMap<String, Object>(paramMap);
		StringBuffer stringBuffer = new StringBuffer();
		for (Map.Entry<String, Object> m : smap.entrySet()) {
			Object value = m.getValue();
			if (value != null && org.apache.commons.lang.StringUtils.isNotBlank(String.valueOf(value))) {
				stringBuffer.append(m.getKey()).append("=")
						.append(m.getValue()).append("&");
			}
		}
		stringBuffer.delete(stringBuffer.length() - 1, stringBuffer.length());

		String argPreSign = stringBuffer.toString();
		log.info("MD5原文:" + argPreSign);
		String signStr = MD5Util.encode(argPreSign).toUpperCase();
		log.info("MD5值：" + signStr);
		return signStr;
	}
	
	/**
	 * 验证签名
	 * 
	 * @param paramMap
	 *            签名参数
	 * @param paySecret
	 *            签名密钥
	 * @return
	 */
	public static boolean checkSign(Map<String, String> paramMap, String paySecret) {
		SortedMap<String, Object> smap = new TreeMap<String, Object>(paramMap);
		StringBuffer stringBuffer = new StringBuffer();
		for (Map.Entry<String, Object> m : smap.entrySet()) {
			Object value = m.getValue();
			if (value != null && org.apache.commons.lang.StringUtils.isNotBlank(String.valueOf(value))) {
				stringBuffer.append(m.getKey()).append("=")
						.append(m.getValue()).append("&");
			}
		}
		stringBuffer.delete(stringBuffer.length() - 1, stringBuffer.length());

		String argPreSign = stringBuffer.append("&secretKey=")
				.append(paySecret).toString();
		log.info("MD5原文:" + argPreSign);
		String signStr = MD5Util.encode(argPreSign).toUpperCase();
		log.info("MD5值：" + signStr);
		return true;
	}


	public static void main(String[] args) {
		String s="	ok";
		char[] sr=s.toCharArray();
		System.out.println(sr.length);
		for (int i=0;i<sr.length;i++){
			System.out.println(sr[i]);
		}
	}

}
